package com.config;

import com.service.CartService;
import com.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author liuronghua
 * @version 1.0
 * @data 2020-09-2020/9/22 10:16
 * @desc
 */
public class IndexInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private TypeService typeService;

    @Autowired
    private CartService cartService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws IOException {
        request.setAttribute("typeList",typeService.getList());

        String uri=request.getRequestURI();
        if (uri.contains("index/cart") || uri.contains("index/order") || uri.contains("index/my")){
            Object user=request.getSession().getAttribute("user");
            if (Objects.isNull(user)){
                response.sendRedirect("login");
                return false;
            }
        }
        return true;
    }
}
